<template>
  <el-row>
    <el-col :span="24">
      <el-card>
        <div style="height: 280px" ref="bar"></div>
      </el-card>
    </el-col>
    <el-col :span="12">
      <el-card>
        <div style="height: 280px" ref="pie"></div>
      </el-card>
    </el-col>
        <el-col :span="12">
      <el-card>
        <div style="height: 280px" ref="line"></div>
      </el-card>
    </el-col>
  </el-row>
</template>

<script>
import * as echarts from "echarts";
import { statisticBar, statisticPie, statisticLine } from "../api/punch";
export default {
  name: "Statistic",
  mounted() {
    statisticBar().then((data) => {
      const xAxis = [];
      const charData = [];
      if (data.success) {
        data.data.forEach((item) => {
          xAxis.push(item.username);
          charData.push(item.count);
        });
        const optionBar = {
          title: {
            text: "打卡次数统计表",
          },
          tooltip: {},
          legend: {
            data: ["用户名"],
          },
          xAxis: {
            data: xAxis,
          },
          yAxis: {},
          series: [
            {
              name: "用户名",
              type: "bar",
              data: charData,
            },
          ],
        };
        const E = echarts.init(this.$refs.bar);
        E.setOption(optionBar);
      }
    });
    statisticPie().then((data) => {
      const pieData = [];
      if (data.success) {
        data.data.forEach((item) => {
          let pie = {
            value: "",
            name: "",
            itemStyle: {},
          };
          pie.value = item.count;
          pie.name = item.type;
          if (item.type === "红色") {
            pie.itemStyle = { color: "#F56C6C" };
          } else if (item.type === "黄色") {
            pie.itemStyle = { color: "#E6A23C" };
          } else {
            pie.itemStyle = { color: "#67C23A" };
          }
          pieData.push(pie);
        });
      }
      const optionPie = {
        title: {
          text: "健康码颜色",
          left: "center",
          top: "center",
        },
        legend: {
          data: ["红色", "黄色", "绿色"],
        },
        series: [
          {
            type: "pie",
            data: pieData,
            radius: ["40%", "70%"],
          },
        ],
      };
      const E = echarts.init(this.$refs.pie);
      E.setOption(optionPie);
    });

    statisticLine().then((data) => {
      const xAxis = [];
      const lineData = [];
      if (data.success) {
        data.data.forEach((item) => {
          xAxis.push(item.date);
          lineData.push(item.count);
        });
      }
      const optionLine = {
        title: {
          text: '近7天打卡记录统计'
        },
        xAxis: {
          data: xAxis
        },
        yAxis: {},
        series: [
          {
            data: lineData,
            type: 'line',
            smooth: true
          }
        ]
      };
      const E = echarts.init(this.$refs.line);
      E.setOption(optionLine)
    });
    // const E = echarts.init(this.$refs.echarts);
    // E.setOption({
    //   title: {
    //     text: "Echarts入门案例",
    //   },
    //   tooltip: {}, //提示框
    //   legend: {  //图例
    //     data: ['销量']
    //   },
    //   xAxis: { //x轴
    //     data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"],
    //   },
    //   yAxis: {}, //y轴
    //   series: [
    //     {
    //       name: "销量",  //该属性需要和legen中的data属性对应
    //       type: "bar",
    //       data: [5, 20, 36, 10, 10, 20],
    //     },
    //   ],
    // });
  },
};
</script>

<style lang="less" scoped>
.el-card {
  margin-top: 20px;
  height: 280px;
  margin: 10px;
}
</style>


